<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;
use Think\Controller;
/**
 * Description of CategoryController
 *
 * @author zhaokai
 */
class CategoryController extends Controller {
    //put your code here
    public function index(){
        $this->display();
    }
    
    public function edit(){
        $id = I("get.id");
        $Category = D("Category");
        if($id != null){
            $category = $Category->where("id=".$id)->select();
            $this->assign("category", $category[0]);
        }
        $categories = $Category->select();
        $this->assign("categories", $categories);
        $this->display();
    }
    
    public function getCategoryList(){
        $Category = D("Category");
        $categories = $Category->order("recordTime desc,id desc")->select();
        $data["data"] = $categories;
        $this->ajaxReturn($data, "JSON");
    }

    public function save(){
        $Category = D("Category");
        $data['parentId'] = $_POST["parentId"];
        $data['status'] = $_POST["status"];
        $data['name'] = $_POST["name"];
        $data['alias'] = $_POST["alias"];
        $id = I("post.id");
        $result = false;
        if($id == ""){
            $data['recordTime'] = date('Y-m-d H:i:s');
            $Category->add($data);
            $result = true;
        }else{
            $Category->where('id = '.$id)->save($data);
            $result = true;
        }
        echo $result;
    }
    
    public function delete(){
        $id = I("post.id");
        $Category = D("Category");
        $result = $Category->where("id = ".$id)->delete();
        $this->ajaxReturn($result);
    }
    
}
